package com.just.springbootmessage.activemq.m2;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.*;

@Configuration
@EnableJms
public class ActivemqConfig {
    @Bean("q1")
    public Queue queue1(){
        return new ActiveMQQueue("q1");
    }
    @Bean("q2")
    public Queue queue2(){
        return new ActiveMQQueue("q2");
    }
    @Bean("t1")
    public Topic topic1(){
        return new ActiveMQTopic("t1");
    }

    // topic模式的ListenerContainer
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(QueueConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        //开启topic支持
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(activeMQConnectionFactory);
        bean.setConcurrency("3-10");     //设置连接数
        bean.setRecoveryInterval(1000L); //重连间隔时间
        return bean;
    }
    // queue模式的ListenerContainer
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerQueue(TopicConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setConnectionFactory(activeMQConnectionFactory);
        return bean;
    }
}
